/**
 * jims
 */
package com.jims.master.modules.operation.web;


import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.operation.entity.*;
import com.jims.master.modules.operation.service.*;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.service.SystemService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 *手术申请
 * Controller
 * @author DT
 * @version 2017-04-06
 */
@Controller
@RequestMapping(value = "${adminPath}/operation/operationApply")
public class OperationApplyController extends BaseController {
    @Autowired
    private OperationApplyService operationApplyService;
    @Autowired
    private OperationDictService operationDictService;
    @Autowired
    private OperationNameApplyService operationNameApplyService;
    @Autowired
    private OperationRoomService operationRoomService;
    @Autowired
    private OrgDeptService orgDeptService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private OperationStaffApplyService operationStaffApplyService;
    @ModelAttribute
    public OperationApply get(@RequestParam(required = false) String id) {
        OperationApply entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = operationApplyService.get(id);
        }
        if (entity == null) {
            entity = new OperationApply();
        }
        return entity;
    }

    /**
     * 门诊手术入口
     * @param operationApply
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(OperationApply operationApply, HttpServletRequest request, HttpServletResponse response, Model model) {
        String clinicId = operationApply.getClinicId();
        String patientId = operationApply.getPatientId();
        String visitId = operationApply.getVisitId();
        Page<OperationApply> page = operationApplyService.findPage(new Page<OperationApply>(request, response), operationApply);
        model.addAttribute("page", page);
        model.addAttribute("clinicId", clinicId);
        model.addAttribute("visitId", visitId);
        model.addAttribute("patientId", patientId);
        OperationDict operationDict = new OperationDict();
        Page<OperationDict> operationDictList = operationDictService.findPage(new Page<OperationDict>(request, response), operationDict);
        model.addAttribute("operationDictList", operationDictList);
        OperationRoom operationRoom = new OperationRoom();
        Page<OperationRoom> operationRoomList = operationRoomService.findPage(new Page<OperationRoom>(request, response), operationRoom);
        if(StringUtils.isNotBlank(operationApply.getOperatingRoomId())){
            OrgDept orgDept = new OrgDept();
            orgDept=orgDeptService.get(operationApply.getOperatingRoomId());
            operationApply.setOperatingRoomName(orgDept.getDeptName());
        }
        model.addAttribute("operationRoomList", operationRoomList);
        model.addAttribute("operationApply", operationApply);
        return "modules/operation/operationApplyIndex";
    }

    /**
     * 加载左侧手术信息列表
     * Controller
     * @version 2017-04-06
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"applyList", ""})
    public String applyList(OperationApply operationApply, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<OperationApply> page = operationApplyService.findPage(new Page<OperationApply>(request, response), operationApply);
        model.addAttribute("page", page);
        model.addAttribute("operationApply", operationApply);
        return "modules/operation/operationApplyView";
    }

    /**
     * @Desciption: 手术名自动补全
     * @author ZYG
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoCompletePrice", ""})
    @ResponseBody
    public List<OperationDict> autoCompletePrice(String q,String limit, OperationDict operationDict) {
        if(q!=null&&!q.equals("")){
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<OperationDict> list = operationApplyService.findAutoCompletePriceist(operationDict, UserUtils.getOrgId(), q, limit);
        return list;
    }

    /**
     * 手术室与手术间的联动
     * Controller
     * @version 2017-04-06
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "findRoom")
    @ResponseBody
    public StringData findRoom(OperationRoom operationRoom) {
        operationRoom.setStatus("0");
        List<OperationRoom> list=operationRoomService.findList(operationRoom);
        Map<String,String> dataMap=new HashMap<String, String>();
        for(OperationRoom operRoom:list){
            dataMap.put(operRoom.getId(),operRoom.getRoomNo());
        }
        StringData data=new StringData();
        data.setDataMap(dataMap);
        return data;
    }

    /**
     * 加载右侧列表
     * @param operationApply
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"applyForm", ""})
    public String applyForm(OperationApply operationApply, HttpServletRequest request, HttpServletResponse response, Model model) {
        OperationDict operationDict = new OperationDict();
        Page<OperationDict> operationDictList = operationDictService.findPage(new Page<OperationDict>(request, response), operationDict);
        model.addAttribute("operationDictList", operationDictList);
        OperationRoom operationRoom = new OperationRoom();
        Page<OperationRoom> operationRoomList = operationRoomService.findPage(new Page<OperationRoom>(request, response), operationRoom);
        if (operationApply != null) {
            if (StringUtils.isNotBlank(operationApply.getId())) {
                operationApply = operationApplyService.get(operationApply);
                OperationNameApply operationNameApply = new OperationNameApply();
                operationNameApply.setApplyId(operationApply.getId());
                List<OperationNameApply> operationNameApplys = operationNameApplyService.findList(operationNameApply);
                operationApply.setOperationNameApplys(operationNameApplys);
                OperationStaffApply operationStaffApply=new OperationStaffApply();
                operationStaffApply.setScheduleId(operationApply.getId());
                OperationStaffApply zdStaffApply=operationApplyService.getStaff(operationStaffApply,"0");
                OperationStaffApply myStaffApply=operationApplyService.getStaff(operationStaffApply,"1");
                OperationStaffApply sxStaffApply=operationApplyService.getStaff(operationStaffApply,"2");
                OperationStaffApply mzStaffApply=operationApplyService.getStaff(operationStaffApply,"3");
                OperationStaffApply yzStaffApply=operationApplyService.getStaff(operationStaffApply,"4");
                operationApply.setZdStaffApply(zdStaffApply);
                operationApply.setMyStaffApply(myStaffApply);
                operationApply.setSxStaffApply(sxStaffApply);
                operationApply.setMzStaffApply(mzStaffApply);
                operationApply.setYzStaffApply(yzStaffApply);
            }
        }
        String clinicId = operationApply.getClinicId();
        String patientId = operationApply.getPatientId();
        if(StringUtils.isNotBlank(operationApply.getDoctorUser())){
            User user=systemService.getUser(operationApply.getDoctorUser());
            operationApply.setDoctorName(user.getName());
        }
        else{
            operationApply.setDoctorUser(UserUtils.getUser().getId());
            operationApply.setDoctorName(UserUtils.getUser().getName());
            operationApply.setOperationDeptId(UserUtils.getUser().getOrgDept().getId());
            operationApply.setOperationName(UserUtils.getUser().getOrgDept().getDeptName());
        }
        if(operationApply.getMakeDate()==null){
            operationApply.setMakeDate(new Date());
        }
        model.addAttribute("clinicId", clinicId);
        model.addAttribute("patientId", patientId);
        if(StringUtils.isNotBlank(operationApply.getOperationDeptId())){
            OrgDept orgDept = new OrgDept();
            orgDept=orgDeptService.get(operationApply.getOperationDeptId());
            operationApply.setOperationName(orgDept.getDeptName());
        }
        if(StringUtils.isNotBlank(operationApply.getOperatingRoomId())){
            OrgDept orgDept = new OrgDept();
            orgDept=orgDeptService.get(operationApply.getOperatingRoomId());
            operationApply.setOperatingRoomName(orgDept.getDeptName());
        }
        model.addAttribute("operationApply", operationApply);
        model.addAttribute("operationRoomList", operationRoomList);
        return "modules/operation/operationApplyForm";
    }
    /**
     * 手术预约保存
     * @param operationApply
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(OperationApply operationApply) {

        int i = 0;
        int t = 1;
        //int k = 0;
        StringData data = new StringData();
        operationApply.setOperStatus("0");
        operationApply.setOperationDeptId(UserUtils.getUser().getOrgDept().getId());//开单科室为登录人所在科室
        operationApply.setDoctorUser(UserUtils.getUser().getId());//开单人为当前登录人
        operationApply.setOrgId(UserUtils.getOrgId());
        OperationNameApply operationNameApply = new OperationNameApply();
        /*for (OperationNameApply nameApply : operationApply.getOperationNameApplys()) {
            nameApply.setOrgId(operationApply.getOrgId());
            OperationDict operationDict=operationDictService.get(nameApply.getOperationId());
            operationNameApply.setOperationName(operationDict.getOperationName());
            operationNameApply.setOperationCode(operationDict.getOperationCode());
            t=operationApplyService.ifOperNameExist(operationNameApply);//判断手术名称是否存在
            k++;
            if(t==0){
                break;
            }
        }*/
        if (t == 0) {
            data.setCode("no");
            //data.setData("第"+k+"行手术名称不存在，请修改手术名称信息！");
            data.setData("手术项目中包含不存在的项目名称，请修改！");
        }else{
            i = operationApplyService.saveOperationApply(operationApply);
            if (i == 1) {
                data.setCode("success");
                data.setData("保存成功");
            } else {
                data.setCode("error");
                data.setData("保存失败");
            }
        }

        return data;
    }
    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(OperationApply operationApply) {
        StringData data=new StringData();
        int i=operationApplyService.deleteAll(operationApply);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
        return data;
    }
}